import axios from 'axios';
import request from '@/utils/request';
import type { Result } from '@/types/global';

// 运营商类型枚举
export const PartnerTypeEnum = {
  PERSONAL: 0, // 加盟
  ENTERPRISE: 1, // 购买
} as const;

// 运营商级别枚举
export const PartnerLevelEnum = {
  NORMAL: 1, // 普通
  SILVER: 2, // 银牌
  GOLD: 3, // 金牌
  DIAMOND: 4, // 钻石
} as const;

// 结算周期枚举
export const SettlementCycleEnum = {
  MONTHLY: 1, // 月结
  QUARTERLY: 2, // 季结
  YEARLY: 3, // 年结
} as const;

// 合伙人角色枚举
export const PartnerRoleEnum = {
  COMPANY: 0, // 公司合伙人
  MARKET: 1, // 市场合伙人
  MANAGER: 2, // 管理合伙人
  COMMISSION: 3, // 分佣合伙人
} as const;

// 状态枚举
export const StatusEnum = {
  DISABLED: 0, // 禁用
  ENABLED: 1, // 启用
} as const;

// 账户状态枚举
export const AccountStatusEnum = {
  FROZEN: 0, // 冻结
  NORMAL: 1, // 正常
} as const;

// 运营商查询参数接口
export interface PartnerQueryParams {
  pageNum?: number;
  pageSize?: number;
  partnerName?: string;
  contactName?: string;
  contactPhone?: string;
  partnerType?: number;
  status?: number;
}

// 运营商实体接口
export interface Partner {
  id: number;
  realName: string;
  mobile: string;
  username?: string;
  password?: string;
  source: number; // 来源 0: 加盟 1：购买
  businessMode?: number;
  redirectPlatformLimit: number;
  managerPartnerDivideRate?: number;
  idCard?: string;
  idCardPhotoBack?: string;
  idCardPhotoPositive?: string;
  remark?: string;
  uuid?: string;
  enabled: boolean;
  headImage?: string;
  merchantId?: number;
  createDate?: string;
  updateDate?: string;
  partnerRole?: number;
  nickName?: string;
  xcxOpenId?: string;

  // 前端展示用属性
  statusName?: string;
  partnerTypeName?: string;
  partnerRoleName?: string;
}

/**
 * 获取运营商列表
 * @param params 查询参数
 * @returns
 */
export function getPartnerList(params: PartnerQueryParams) {
  return axios.get<
    Result<{
      records: Partner[];
      total: number;
      size: number;
      current: number;
      pages: number;
    }>
  >('/api/v1/partner/search', {
    params, // 直接传递参数对象，不要包裹额外对象
  });
}

/**
 * 获取运营商详情
 * @param id 运营商ID
 * @returns
 */
export function getPartnerDetail(id: number) {
  return request.get<Result<Partner>>(`/api/v1/partner/${id}`);
}

/**
 * 添加运营商
 * @param data 运营商数据
 * @returns
 */
export function addPartner(data: Partial<Partner>) {
  return request.post<Result<boolean>>('/api/v1/partner', data);
}

/**
 * 更新运营商
 * @param data 运营商数据
 * @returns
 */
export function updatePartner(data: Partial<Partner>) {
  return request.put<Result<boolean>>('/api/v1/partner', data);
}

/**
 * 删除运营商
 * @param id 运营商ID
 * @returns
 */
export function deletePartner(id: number) {
  return request.delete<Result<boolean>>(`/api/v1/partner/${id}`);
}

/**
 * 启用运营商
 * @param id 运营商ID
 * @returns
 */
export function enablePartner(id: number) {
  const data = {
    id,
    enabled: true,
  };
  return request.put<Result<boolean>>('/api/v1/partner', data);
}

/**
 * 禁用运营商
 * @param id 运营商ID
 * @returns
 */
export function disablePartner(id: number) {
  const data = {
    id,
    enabled: false,
  };
  return request.put<Result<boolean>>('/api/v1/partner', data);
}

// 获取所有客户列表（不分页）
export function getAllPartners(params?: { status?: number }) {
  return request.get('/api/v1/partner/list', { params });
}
